<template>
    <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
        class="demo-ruleForm" :size="formSize" status-icon>
        <el-form-item label="商品名称" prop="name">
            <el-input v-model="ruleForm.name" />
        </el-form-item>


        <el-form-item label="商品品牌" prop="region">
            <el-select v-model="ruleForm.region" placeholder="请选择品牌">
                <el-option label="请选择" value="" />
                <el-option v-for="a in list" :label="a.Gbname" :value="a.Gbname" />
            </el-select>
        </el-form-item>
        <el-form-item label="商品分类" prop="count">
            <el-cascader v-model="value" :options="Goodstype" :props="props" @change="handleChange" />
        </el-form-item>


        <el-form-item label="商品价格" prop="name">
            <el-input v-model="ruleForm.name" />
        </el-form-item>


        <el-form-item label="商品图片" prop="name">

        </el-form-item>


        <el-form-item label="商品状态" prop="resource">
            <el-radio-group v-model="ruleForm.resource">
                <el-radio value="Sponsorship">启用</el-radio>
                <el-radio value="Venue">禁用</el-radio>
            </el-radio-group>
        </el-form-item>



        <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
                保存
            </el-button>
            <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import axios from 'axios'

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()



//响应式数据
const ruleForm = reactive({
    name: '',
    region: '',
    count: '',
    date1: '',
    date2: '',
    delivery: false,
    location: '',
    type: [],
    resource: '',
    desc: '',
})



//表单数据验证
const rules = reactive<FormRules>({
    name: [
        { required: true, message: '商品名称不能为空', trigger: 'blur' },
        { min: 3, max: 10, message: '商品名称为3-10个字符', trigger: 'blur' },
    ],
    region: [
        {
            required: true,
            message: 'Please select Activity zone',
            trigger: 'change',
        },
    ],
    count: [
        {
            required: true,
            message: 'Please select Activity count',
            trigger: 'change',
        },
    ],
    date1: [
        {
            type: 'date',
            required: true,
            message: 'Please pick a date',
            trigger: 'change',
        },
    ],
    date2: [
        {
            type: 'date',
            required: true,
            message: 'Please pick a time',
            trigger: 'change',
        },
    ],
    location: [
        {
            required: true,
            message: 'Please select a location',
            trigger: 'change',
        },
    ],
    type: [
        {
            type: 'array',
            required: true,
            message: 'Please select at least one activity type',
            trigger: 'change',
        },
    ],
    resource: [
        {
            required: true,
            message: 'Please select activity resource',
            trigger: 'change',
        },
    ],
    desc: [
        { required: true, message: 'Please input activity form', trigger: 'blur' },
    ],
})

//提交事件
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!')
        } else {
            console.log('error submit!', fields)
        }
    })
}

//重置事件
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}))

//创建生命周期函数钩
onMounted(() => {
    Gbshow();
    Gtypeshow();
})

let list: any = ref([])
let Goodstype: any = ref([])

const Gbshow = () => {
    axios({
        url: "/api/Goods/ShowGb",
        method: 'get'
    })
        .then(res => {
            list.value = res.data.Data;
        })
}


const Gtypeshow = () => {
    axios({
        url: "/api/Goods/ShowGt?pid=0",
        method: 'get'
    })
        .then(res => {
            Goodstype.value = res.data.Data;
        })
}
const value:any = ref([])

const props = {
  expandTrigger: 'hover' as const,
}

const handleChange = (value:any) => {
  console.log(value)
}

</script>